package logic.configuration;


import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class XMLConfigurationLoader extends AbstractConfigurationLoader {
	
	static Logger logger = Logger.getLogger(XMLConfigurationLoader.class.getName());	
	
	public XMLConfigurationLoader() { }

	@Override
	public Configuration parse(File configurationFile) 
			throws ParserConfigurationException, SAXException, IOException {
		
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser saxParser = factory.newSAXParser();
		ConfigXMLDefaultSAXHandler handler = new ConfigXMLDefaultSAXHandler();
		saxParser.parse(configurationFile, handler);
		Configuration configuration = handler.getConfiguration();
		return configuration;
	}
	
	//unit tests
	public static void main(String[] args) {
		//TODO uzycie fabryki
		XMLConfigurationLoader xmlLoader = new XMLConfigurationLoader();
		Configuration configuration = null;
		try {
			configuration = xmlLoader.parse(new File("MyTC-config.xml"));		//TODO do konstruktora
			
			//for (String s : toolbarCommands) {
				logger.info(configuration.toString());
			//}
		} catch (ParserConfigurationException | SAXException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
